<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * OPE control for K9
 *
 */
class K9_Deploy_Controller extends CI_Controller
{
	protected $uid = 0;
	protected $token = null;
	public $user = null;
    public $language = null;

	public function __construct() {
		
		parent::__construct();

		$this->load->helper('url');
		$this->load->helper('cookie');
		$this->load->helper('form');
		$this->load->helper('ajax');
		$this->load->library('session');
		
		$this->load->library('deploy/deploy_lib');
		$this->load->library('deploy/acl_lib');
		
		$this->load->model('deploy/role_model');
		$this->load->model('deploy/chcode_group_model');

        $this->language = get_language($this->config->item('language'));

		$this->_islogin();
	}
	
	protected function _islogin() {
		
		$this->uid = $this->session->userdata('uid');
		
		if (empty ( $this->uid )) {
//			redirect('login');
			return false;
		}
		
		$this->user = $this->deploy_lib->get_account_by_id( $this->uid );
		if (empty ( $this->user )) {
//			redirect('login');
			return false;
		}
		
		if($this->user->role > 0)
			$this->acl_lib->set_role($this->role_model->get_role_by_id($this->user->role));
		
		return true;
	}
	
	/**
	 * 检查权限
	 *
	 * @access  protected
	 * @return  void
	 */
	protected function _check_permit()
	{
		if ( ! $this->acl_lib->permit($this->uid))
		{
			$this->_message('对不起，你没有访问这里的权限！', '', FALSE);
		}
	}
	
	/**
	 * 信息提示
	 *
	 * @access  public
	 * @param $msg
	 * @param string $goto
	 * @param bool $auto
	 * @param string $fix
	 * @param int $pause
	 * @return  void
	 */
	public function _message($msg, $goto = '', $auto = TRUE, $fix = '', $pause = 6000)
	{
		if($goto == '')
		{
			$goto = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : site_url();
		}
		else
		{
			$goto = strpos($goto, 'http') !== false ? $goto : backend_url($goto);
		}
		$goto .= $fix;
		$this->_template($this->language.'/sys_message', array('msg' => $msg, 'goto' => $goto, 'auto' => $auto, 'pause' => $pause));
		echo $this->output->get_output();
		exit();
	}
	
	/**
	 * load view
	 *
	 * @access  protected
	 * @param   string
	 * @param   array
	 * @return  void
	 */
	protected function _template($template, $data = array())
	{
		$data['tpl'] = $template;
        $data['language'] = $this->language;
		$this->load->view($this->language.'/entry', $data);
	}
}
